//
//  XhwPlatFormView.m
//  BABaseProject
//
//  Created by 熊宏伟 on 2018/1/10.
//  Copyright © 2018年 博爱之家. All rights reserved.
//

#import "XhwPlatFormView.h"


@interface XhwPlatFormView()

@property (nonatomic, strong) NSMutableArray * shareTitleArray;

@property (nonatomic, strong)NSMutableArray * shareImageArray;

@property (nonatomic, strong) UIView *viewSheet;

@end

@implementation XhwPlatFormView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
//        _shareTitleArray = [[NSMutableArray alloc] initWithArray:@[@"分享给朋友",@"分享到动态",@"收藏",@"在浏览器中打开",@"邮件",@"复制链接",@"调整字体",@"刷新",@"举报"]];
//        _shareImageArray = [[NSMutableArray alloc] initWithArray:@[@"ico_adv_share1",@"ico_adv_share2",@"ico_adv_share3",@"ico_adv_share4",@"ico_adv_share5",@"ico_adv_share6",@"ico_adv_share7",@"ico_adv_share8",@"ico_adv_share9"]];
        _shareTitleArray = [[NSMutableArray alloc] initWithArray:@[@"分享给朋友",@"分享到动态",@"在浏览器中打开",@"邮件",@"复制链接",@"调整字体",@"刷新",@"举报"]];
        _shareImageArray = [[NSMutableArray alloc] initWithArray:@[@"ico_adv_share1",@"ico_adv_share2",@"ico_adv_share4",@"ico_adv_share5",@"ico_adv_share6",@"ico_adv_share7",@"ico_adv_share8",@"ico_adv_share9"]];
        
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tappedCancel)];
        [self addGestureRecognizer: tapGes];
        
        
        
        
        _viewSheet = [[UIView alloc] init];
        [self addSubview:_viewSheet];
        _viewSheet.backgroundColor = [UIColor whiteColor];
        CGFloat f_w = [UIScreen mainScreen].bounds.size.width/5;
        _viewSheet.frame = CGRectMake(0, self.height, self.width, ((_shareTitleArray.count-1)/5+1)*f_w + 50);
        
        UIButton *cancelBtn = [[UIButton alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self
                                                                             action:@selector(cancelBtnClick)];
        cancelBtn.userInteractionEnabled = YES;
        [cancelBtn addGestureRecognizer:tap];

        cancelBtn.frame = CGRectMake(0, _viewSheet.frame.size.height-50, [UIScreen mainScreen].bounds.size.width, 50);
        [self.viewSheet addSubview:cancelBtn];
        cancelBtn.backgroundColor = [UIColor grayColor];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        
        for (NSInteger i = 0; i < [_shareTitleArray count]; i++) {
            UIImage *img = [UIImage imageNamed:_shareImageArray[i]];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = i;
            btn.frame = CGRectMake(f_w*(i%5), f_w*(i/5), f_w, f_w);
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn setImage:img forState:UIControlStateNormal];
            [btn setTitle:_shareTitleArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            btn.imageView.contentMode = UIViewContentModeCenter;
            btn.titleLabel.contentMode = UIViewContentModeCenter;
            NSDictionary * titleAttrDic = [NSDictionary dictionaryWithObjectsAndKeys:btn.titleLabel.font,NSFontAttributeName, nil];
            CGSize titleSize = CGSizeMake([btn.titleLabel.text sizeWithAttributes:titleAttrDic].width, btn.titleLabel.font.lineHeight);
            btn.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height, 0, 0, -titleSize.width);
            btn.titleEdgeInsets = UIEdgeInsetsMake(img.size.height, -img.size.width, 0, 0);
            btn.titleEdgeInsets = UIEdgeInsetsMake(img.size.height, -img.size.width, 0, 0);
            [self.viewSheet addSubview:btn];
        }
        [UIView animateWithDuration:0.25 animations:^{
            self.viewSheet.frame = CGRectMake(0, self.height-self.viewSheet.height, self.viewSheet.width, self.viewSheet.height);
        }];
    }
    return self;
}

- (void)tappedCancel {
    [UIView animateWithDuration:0.25 animations:^{
        self.viewSheet.frame = CGRectMake(0, self.height, self.viewSheet.width, self.viewSheet.height);
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

- (void)cancelBtnClick {
    [self tappedCancel];
}

- (void)btnClick:(UIButton *)sender {
    
    [self tappedCancel];
    
    if ([self.delegate respondsToSelector:@selector(btnClick:)]) {
        [self.delegate btnClick:sender];
    }
        
}

@end
